﻿//----------------------------------------------------------------
//CodeArt.
//
//Descrpton:
//
//Author:jianyi0115@163.com  
//Created: 2009-10-27
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Microsoft.SharePoint;

namespace CodeArt.SharePoint.MultiLanSupport
{
    /// <summary>
    /// 实现拦截所有aspx请求
    /// </summary>
    public class MultiLanSupportModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
        }

        private System.Web.HttpApplication _HttpApp;

        public void Init(HttpApplication context)
        {
            _HttpApp = context;

            context.AcquireRequestState += new EventHandler(_HttpApp_AcquireRequestState);          
           // context.BeginRequest += new EventHandler(context_BeginRequest);                     
        }

        void _HttpApp_AcquireRequestState(object sender, EventArgs e)
        {
            string verb = _HttpApp.Request.HttpMethod.ToLower();
            if ( !( verb == "get" || verb == "post" ) )
                return;         

            string url = _HttpApp.Request.RawUrl.Split('?')[0].ToLower();

            //切换线程语言
            //if (url.EndsWith(".aspx"))
                //UICultureManager.CurrentInstance.SetThreadCulture();

            //如果是aspx文件，才进行翻译
            if (url.EndsWith(".aspx") && url.IndexOf("/_layouts/") == -1)
            {
                SPContext spContext = SPContext.Current;
                if (spContext == null)
                    return;           

                UICultureManager.CurrentInstance.SetThreadCulture();

                if( LanMapper.EnableLanTrans( spContext.Web ))
                    _HttpApp.Response.Filter = new MultiLanFilter(_HttpApp.Response.Filter);
            }
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
           
        }      

        #endregion
    }
}
